/**
 * 
 */
package stc.skymobi.content.transformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import stc.skymobi.content.Bean2PathTransformer;


/**
 * @author hp
 *
 */
public class DefaultBean2PathTransformer implements Bean2PathTransformer {

    private static final Logger logger = LoggerFactory.getLogger(DefaultBean2PathTransformer.class);
    
    private Map<String, String> table = new HashMap<String, String>();

    public List<String> propertyNames = new ArrayList<String>();
    
    public void clearTable() {
        table.clear();
    }
    
    public void addRow(String key, String path) {
        table.put(key, path);
    }
    
    /**
     * @param propertyNames the propertyNames to set
     */
    public void setPropertyNames(List<String> propertyNames) {
        this.propertyNames.clear();
        this.propertyNames.addAll(propertyNames);
    }

    /* (non-Javadoc)
     * @see com.skymobi.util.Transformer#transform(java.lang.Object)
     */
    public String transform(Object from) {
        try {
            String key = "";
            String split = "";
            for ( String name : this.propertyNames ) {
                    key += split + BeanUtils.getProperty(from, name);
                    split = ":";
            }
            
            String path = table.get(key);
            
            if ( logger.isDebugEnabled() ) {
            	logger.debug("transform : " + from + "-> key is '" + key + "', and path is ["
                        + path + "]");
            }
            
            return  path;
        } catch (Exception e) {
        	logger.error("transform:", e);
        }
        
        return null;
    }

}
